<?php
/**
 * @title 421. 数组中两个数的最大异或值 - 超时
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class TreeNode{
    public $val = null;
    public $left = null;
    public $right = null;

    function __construct($val = null, $left = null, $right = null)
    {
        $this->val = $val;
        $this->left = $left;
        $this->right = $right;
    }
}

class Solution {
    const NUM_LEN = 31;

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function findMaximumXOR($nums) {
        /**
         * @since 2023-11-10 树
         */
        $tree = new TreeNode();

        /**
         * @since 2023-11-10 数组长度，2*10^5
         */
        $len = count($nums);

        /**
         * @since 2023-11-10 放到树的子节点上
         */
        $binArray = [];
        for ($i=0; $i<$len; $i++){
            /**
             * @since 2023-11-10 转成二进制字符串
             */
            $num = $nums[$i];
            $numBin = self::GetBin($num);
            $binArray[] = $numBin;
            // echo "num={$num}, {$numBin} = (", strlen($numBin), ")", PHP_EOL;

            /**
             * @since 2023-11-10 子节点
             *      null
             *     /    \
             *    0      1
             *   / \    / \
             *  0   1  0   1
             */
            $head = $tree;
            for($h=0; $h<self::NUM_LEN; $h++){
                /**
                 * @since 2023-11-10 0=>left, 1=>right
                 */
                if($numBin[$h] == 0){
                    if($head->left === null){
                        $head->left = new TreeNode();
                    } else {}
                    $head = $head->left;
                } else {
                    if($head->right === null){
                        $head->right = new TreeNode();
                    } else {}
                    $head = $head->right;
                }
            }
            $head->val = $num;
        }

        /**
         * @since 2023-11-10 每个数字对应异或最大值
         */
        $maxXor = 0;
        for($i=0; $i<$len; $i++){
            /**
             * @since 2023-11-10 转成二进制字符串
             */
            $num = $nums[$i];
            $numBin = $binArray[$i];
            // echo "num={$num}, {$numBin}", PHP_EOL, PHP_EOL;

            /**
             * @since 2023-11-10 二叉树找到异或最大值
             */
            $head = $tree;
            for($h=0; $h<self::NUM_LEN; $h++){
                if($numBin[$h] == 0){
                    /**
                     * @since 2023-11-10 右子树优先
                     */
                    if($head->right !== null){
                        $head = $head->right;
                    } else {
                        $head = $head->left;
                    }
                } else {
                    /**
                     * @since 2023-11-10 左子树优先
                     */
                    if($head->left !== null){
                        $head = $head->left;
                    } else {
                        $head = $head->right;
                    }
                }
            }

            /**
             * @since 2023-11-10 异或
             */
            // echo "{$num} ^ ". $head->val, PHP_EOL;
            $maxXor = max($maxXor, $num ^ $head->val);
        }

        /**
         * @return
         */
        return $maxXor;
    }

    /**
     * @title 返回31位二进制字符串
     * @author start2004
     * @since 2023-11-10 Created
     *
     * @param Int $num
     * @return string
     */
    function GetBin($num = 0){
        /**
         * @since 2023-11-10 转换二进制
         */
        $numBin = strval(decbin($num));

        /**
         * @since 2023-11-10 高位不够补0
         */
        $numBin = str_repeat("0", self::NUM_LEN - strlen($numBin)) . $numBin;

        /**
         * @return
         */
        return $numBin;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.04.php
 */
$datas = [
    [[3,10,5,25,2,8]], // 28
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->findMaximumXOR(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
